package com.zsr.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.zsr.example.config.OssConfigProperties;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.service.OssService;
import com.zsr.example.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.zsr.example.util.constants.SysLogConstants.UPLOAD;

/**
 * @author zsr
 * @date 2021/10/17 18:27
 */
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {

    @Autowired
    private OssService ossService;

    /**
     * 上传头像的方法
     * @param file
     * @param host
     * @return
     */
    @OptLog(optType = UPLOAD,optDesc = "上传文件")
    @PostMapping
    public JSONObject uploadOssFile(
                           @RequestParam("file") MultipartFile file,
                           @RequestParam(value = "host", required = false) String host) {
        if(!StringUtils.isEmpty(host)){
            OssConfigProperties.FILE_HOST = host;
        }
        String url = ossService.uploadFileAvatar(file,host);
        return CommonUtil.successJson(url);
    }
}
